<?php
namespace app\html5\controller;
use app\Db2;
use app\H5AuthController;

// class Yaofang{
class Yaofang extends H5AuthController{

    public function get_yaofang_info(){
        // $user = $this->user;
        $id = input('id');

        if(!$id || $id == 'undefined') return rejson(302,'id不能为空');

        $where = [
            'id' => $id
        ];

        $data = Db2::get_data('yaofang',[$where]);

        $glyf =  Db2::get_datalist("zy_cengji",[['yfid'=>$id]],['id','name'],);

        $data['guanlian_zd'] = $glyf;

        return rejson(200, '操作成功', $data);
    }

    public function get_yaofang_list(){
        // $user = $this->user;

        $curpage = input(('curpage'));
        $listnum = input(('listnum'));

        $curpage = $curpage?$curpage:0;
        $listnum = $listnum?$listnum:20;

        $list = Db2::get_datalist_page("yaofang", null, $curpage, $listnum, '', 'id desc');

        // $list = Db2::get_datalist("yaofang",[[]],['id','pid','name'],);

        return rejson(200, '操作成功', $list);
    }

    public function save_yaofang(){
        // $user = $this->user;
        // $userid = $user['id'];

        $yfid = input('id');
        $name = input('name');
        $guanlian_zd_ids = input('guanlian_zd_ids');
        $text1 = input('text1');
        $price = input('price');

        // die($guanlian_zd_ids);

        if(!$name || $name == 'undefined') return rejson(302,'请填写药方名称');
        if(!$text1 || $text1 == 'undefined') return rejson(302,'药方不能为空');


        $data = [
            'uid' => 1,
            'name' => $name,
            'text1' => $text1,
            'price' => $price,
            'add_time' => time(),
        ];

        if ($yfid) {
            Db2::update_data("yaofang",[['id '=>$yfid]],$data);
        } else {
            $yfid = Db2::insert_data('yaofang',$data);
        }
        
        Db2::update_data("zy_cengji",['id in ('.$guanlian_zd_ids.')'],['yfid' => $yfid]);

        return rejson(200,'保存成功');
    }

    public function addzd2yaofang(){
        $user = $this->user;

        $zdid = input('zdid');
        $yfid = input('yfid');

        if(!$zdid || $zdid == 'undefined') return rejson(302,'必填字段缺失');
        if(!$yfid || $yfid == 'undefined') return rejson(302,'必填字段缺失');

        Db2::update_data("zy_cengji",[['id' => $zdid]],['yfid' => $yfid]);
        return rejson(200, '操作成功');
    }
}

